package com.example.asus.mydiary;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus.Test.MainActivity;
import com.example.asus.db.domain.Note;
import com.example.asus.db.gen.NoteDao;
import com.example.asus.db.service.GreenDaoManager;
import com.example.asus.db.service.SysApplication;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class View_and_Edit_Diary extends AppCompatActivity {
    Note note,checkNote;
    private List<Note> noteData = null;
    private EditText etTile,etBody;
    private NoteDao noteDao;
    private Date date;
    private String sDate;
    DiaryRecycler diaryRecycler;
    private DiaryRecyclerAdapter diaryRecyclerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_and__edit__diary);
        SysApplication.getInstance().addActivity(this);
        initView();
        initData();
        display();

    }

    /**
     *  初始化控件
     */
    public void initView(){
        etTile = (EditText) findViewById(R.id.viewEdit_title);
        etBody = (EditText)findViewById(R.id.viewEdit_body);
    }

    /**
     *  初始化数据
     */
    public void initData(){
        note = new Note();
        checkNote = new Note();
        GreenDaoManager.InitDao(this,"diary.db");
        noteDao = GreenDaoManager.getDaoSession().getNoteDao();
        // 接受活动传递过来的信息
        note = (Note)getIntent().getParcelableExtra("currentData");
        checkNote = noteDao.loadByRowId(note.getId());  // 不能再使用上述语句，否则两个变量共用一块地址

    }

    public void display(){
        etTile.setText(note.getTitle());
        etBody.setText(note.getBody());
    }

    /**
     *  监听back键按下事件
     *  重写该事件时，应该把super.onBackPressed();去掉，否则机会直接返回上一个页面
     *  出现对话框也没有时间进行选择
     */
    @Override
    public void onBackPressed() {
        /**
         *  判断数据是否改动
         *  若改动则询问用户是否保存
         *  保存则重新保存到数据库并返回主页面
         *  否则
         */
        checkNote.setTitle(etTile.getText().toString());
        checkNote.setBody(etBody.getText().toString());
        /**
         *  有改动则询问对话框
         *  这里如果直接note.equals(checknote)则无论如何都返回false
         */
        if (!(note.getTitle().equals(checkNote.getTitle())) || !(note.getBody().equals(checkNote.getBody()))) {
            updateNormalDialog();
//            noteData = noteDao.loadAll();  //读取当前数据库所有数据条数
//            DiaryRecyclerAdapter diaryRecyclerAdapter = new DiaryRecyclerAdapter(noteData);
//            diaryRecyclerAdapter.notifyItemChanged(Integer.parseInt(String.valueOf(note.getId())));
//            Intent intent = new Intent(View_and_Edit_Diary.this,DiaryRecycler.class);
        }
        //无修改则直接返回
        else {
            /**
             *  返回到日记页面
             */
            Intent intent = new Intent(View_and_Edit_Diary.this, DiaryRecycler.class);
            startActivity(intent);
            finish();
        }
    }

    /**
     *  初始化菜单
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_save:
                checkNote.setTitle(etTile.getText().toString());
                checkNote.setBody(etBody.getText().toString());
                /**
                 *  有改动则询问对话框
                 *  这里如果直接note.equals(checknote)则无论如何都返回false
                 */
                if (!(note.getTitle().equals(checkNote.getTitle())) || !(note.getBody().equals(checkNote.getBody()))) {
                    updateNormalDialog();
                }
                //无修改则直接返回
                else {
                    Intent intent = new Intent(View_and_Edit_Diary.this, DiaryRecycler.class);
                    startActivity(intent);
                    finish();
                }
                break;
            case R.id.menu_return:
                Intent intent = new Intent(View_and_Edit_Diary.this,DiaryRecycler.class);
                startActivity(intent);
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
     *  确定是否修改对话框
     */
    private void updateNormalDialog(){
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(View_and_Edit_Diary.this);
        normalDialog.setIcon(R.drawable.btn_save);
        normalDialog.setTitle("是否保存修改？");
//        normalDialog.setMessage("是否保存修改？");
        normalDialog.setNeutralButton("保存", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        noteData = new ArrayList<Note>();
                        date = new Date();
                        sDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
                        checkNote.setDate(sDate);
                        noteDao.update(checkNote);
                        /**
                         *  返回到日记页面
                         */
                        Intent intent = new Intent(View_and_Edit_Diary.this,DiaryRecycler.class);
                        startActivity(intent);
                        finish();
                    }
                });
        normalDialog.setNegativeButton("不保存",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        /**
                         *  返回到日记页面
                         */
                        Intent intent = new Intent(View_and_Edit_Diary.this,DiaryRecycler.class);
                        startActivity(intent);
                        finish();
                    }
                });
        normalDialog.setPositiveButton("取消",
                new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
            }
        });
        // 显示
        normalDialog.show();
    }
}
